//
//  SMMessageListViewController.m
//  SaveMoney
//
//  Created by user on 2018/11/29.
//  Copyright © 2018年 Sunflower. All rights reserved.
//

#import "SMMessageListViewController.h"

#import "SMMessageListTableViewCell.h"
#import "NSAttributedString+YYText.h"

@interface SMMessageListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *maskView;

@property (nonatomic, copy) NSArray *listArray;

@end

@implementation SMMessageListViewController

- (void)viewDidLoad {
    [super viewDidLoad]; 
    
    self.navigationItem.title = _moneyModel.saveMoneyTitle;
    
    _listArray = [SMSaveMoneyMessageModel findByCriteria:[NSString stringWithFormat:@"Where saveMoneyTitle = '%@' ORDER BY creationTime DESC",_moneyModel.saveMoneyTitle]];
    if (_listArray.count == 0) {
        [_maskView setHidden:NO];
    } else {
        [_maskView setHidden:YES];
    }
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SBScaleNumber(120);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SMMessageListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SMMessageListTableViewCell" forIndexPath:indexPath];
    
    SMSaveMoneyMessageModel *model = _listArray[indexPath.row];
    
    UIImage *image = [UIImage imageNamed:@"meiricunqian"];
    
    NSMutableAttributedString *total = [[NSMutableAttributedString alloc] init];
    
    if (model.type == SaveMoney) {
        image = [UIImage imageNamed:@"shouru"];
        NSString *title = [NSString stringWithFormat:@"存入：￥%ld",(long)model.saveMoney];
        
        total = [total initWithString:title];
        
        [total yy_setColor:[UIColor redColor] range:NSMakeRange(3, total.length-3)];
        [total yy_setFont:[UIFont systemFontOfSize:SBScaleNumber(12)] range:NSMakeRange(3, 1)];
        
        
    } else if (model.type == CostMoney) {
        image = [UIImage imageNamed:@"zhichu"];
        NSString *title = [NSString stringWithFormat:@"支取：￥%ld",(long)model.costMoney];
        total = [total initWithString:title];
        
        [total yy_setColor:[UIColor redColor] range:NSMakeRange(3, total.length-3)];
        [total yy_setFont:[UIFont systemFontOfSize:SBScaleNumber(12)] range:NSMakeRange(3, 1)];
    } else {
        image = [UIImage imageNamed:@"meiricunqian"];
        NSString *title = [NSString stringWithFormat:@"每日存入：￥%ld",(long)model.saveMoney];
        total = [total initWithString:title];
        
        [total yy_setColor:[UIColor redColor] range:NSMakeRange(5, total.length-5)];
        [total yy_setFont:[UIFont systemFontOfSize:SBScaleNumber(12)] range:NSMakeRange(5, 1)];
    }
    
    cell.myImage.image = image;
     
    cell.titleLabel.attributedText = total;
    
    
    NSMutableAttributedString *bitTotal = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"当前剩余金额：￥%ld",(long)model.bitMoney]];
    
    [bitTotal yy_setColor:[UIColor redColor] range:NSMakeRange(7, bitTotal.length-7)];
    [bitTotal yy_setFont:[UIFont systemFontOfSize:SBScaleNumber(10)] range:NSMakeRange(7, 1)];
    
    cell.bitLabel.attributedText = bitTotal;
    
    cell.timeLabe.text = model.creationTime;
    
    cell.message.text = model.message;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
